package com.shelpe.services.sinbad.controller.dto;

import java.util.Date;

import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.controller.EffectMetricDto;
import com.shelpe.services.sinbad.dto.AdgroupAndRptAdgroupDto.rptAdgroupVo;
import com.shelpe.services.sinbad.entity.jpa.Adgroup;
import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.model.AdgroupWithRptAdgroupDto;
import com.shelpe.services.sinbad.service.simbaproxy.SimbaProxyBaseDto;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@JsonInclude(Include.NON_NULL)
@EqualsAndHashCode(callSuper = true)
public class AdgroupItem extends BaseDTO {
	@JsonProperty("adgroup_id")
	private long adgroupID;

	@JsonProperty("user_id")
	private long userID;
	private String nick;
	@JsonProperty("campaign_id")
	private long campaignID;
	@JsonProperty("campaign_name")
	private String campaignName;
	@JsonProperty("auto_type")
	private String autoType;
	@JsonProperty("product_id")
	private long productID;
	@JsonProperty("online_status")
	private String onlineStatus;
	private int status;
	@JsonProperty("maintain_date")
	@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Shanghai")
	private Date maintainedDate;
	@JsonProperty("next_time")
	@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
	private Date nextTime;

	private int limit;
	@JsonProperty("title")
	private String firstCreativeTitle;
	@JsonProperty("img_url")
	private String firstCreativeImg;
	@JsonProperty("default_price")
	private int defaultPrice;

	@JsonProperty("category_name")
	private String categoryName;// 类目级分类名称
	@JsonProperty("drive_type")
	private String driveType;
	@JsonProperty("adgroup_status")
	private String adgroupStatus;// 1:下架 2:审核下线3:暂停 4:推广
	// 养分标示
	@JsonProperty("mobile_mark")
	private int mobileMark; // 1 可以养分 2养分中
	// 养分宝贝ID(一个宝贝可以多次养分)
	@JsonProperty("mobile_adgroup_id")
	private long mobileAdgroupID;
	// 可养分的关键词个数
	@JsonProperty("mobile_keywords_num")
	private int mobileKeywordsNum;

	private SimbaProxyBaseDto.Error error;

	@Override
	public void form(SimbaProxyBaseDto.Error error) {
		super.form(error);
	}

	@JsonProperty("effect_data")
	private EffectMetricDto effect;

	private rptAdgroupVo pc;
	private rptAdgroupVo mobile;
	private rptAdgroupVo all;

	public void from(AdgroupWithRptAdgroupDto adgroup, rptAdgroupVo rpt) {
		BeanUtils.copyProperties(adgroup, rpt);
		rpt.setCtr(adgroup.getCtr() / 100);
	}

	public void from(Adgroup po) {
		this.setAdgroupID(po.getAutoID());
		this.setNick(po.getNick());
		this.setUserID(po.getUserID());
		this.setCampaignID(po.getCampaignID());
		this.setProductID(po.getProductID());
		this.setOnlineStatus(po.getOnlineStatus().name());
		this.setStatus(po.getStatus().getValue());
		this.setMaintainedDate(po.getMaintainedDate());
		this.setNextTime(po.getNextTime());
		this.setLimit(po.getLimit());
		this.setFirstCreativeImg(po.getProductUrl());
		this.setFirstCreativeTitle(po.getProductName());
		this.setDefaultPrice(po.getDefaultPrice());
		this.setDriveType(po.getDriveType().name());
	}

	public void from(Campaign po) {
		this.setCampaignName(po.getTitle());
		this.setAutoType(po.getAutoType().name());
	}

	public void fromAdgroup(AdgroupWithRptAdgroupDto dto) {
		this.campaignID = dto.getCamapaignId();
		this.adgroupID = dto.getAdgroupId();
		this.firstCreativeTitle = dto.getTitle();
		this.firstCreativeImg = dto.getImgUrl();
		this.productID = dto.getProductId();
		if (dto.getDriveType() == 1 || dto.getDriveType() == 2 || dto.getDriveType() == 4) {
			this.driveType = "auto";
		} else {
			this.driveType = "manual";
		}
		if (dto.getAdgroupStatus() == 4) {
			this.adgroupStatus = "online";
		} else if (dto.getAdgroupStatus() == 3) {
			this.adgroupStatus = "offline";
		} else if (dto.getAdgroupStatus() == 1) {
			this.adgroupStatus = "pro_offline";
		} else {
			this.adgroupStatus = "check_offline";
		}
	}
}
